package com.example.jiaju.controller;
import com.example.jiaju.enums.ResultEnum;
import com.example.jiaju.exception.SMException;
import com.example.jiaju.form.HostNameForm;
import com.example.jiaju.service.impl.HostServiceImpl;
import com.example.jiaju.until.ResultVOUntil;
import com.example.jiaju.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/Hosts")
public class HostController {
    @Autowired
    private HostServiceImpl hostService;

    @PostMapping("/updateHostName")
    public ResultVO updateHostName(@RequestBody @Valid HostNameForm hostNameForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        hostService.updateName(hostNameForm.getPhone(),hostNameForm.getIeee(),hostNameForm.getNewName());

        return ResultVOUntil.success();
    }
}
